import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import AutoImport from 'unplugin-auto-import/vite'
import autoprefixer from "autoprefixer"
import postcsspxtorem from "postcss-pxtorem"

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
      ]
    }),
  ],
  base: '/',
  publicDir: 'public',
  server: {
    host: '0.0.0.0', // 默认localhost
    port: 5173,
    // open: true,
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:4523/m1/5461420-5136757-default',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    }
  },
  build: {
    outDir: "dist",
    assetsDir: "assets",
    sourcemap: false,
    minify: 'terser',
    terserOptions: {
      // 生产环境移除console、debugger
      compress: {
        drop_console: true,   // 默认false
        drop_debugger: true,  // 默认true
      },
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    postcss: {
      plugins: [
        postcsspxtorem({
          rootValue: 16, // 设计稿宽度的1/ 10 例如设计稿按照 1920设计 此处就为192
          propList: ["*", "!border"], // 除 border 外所有px 转 rem
          selectorBlackList: [".el-"], // 过滤
          exclude: /(node_module)/,
          unitPrecision: 5, //允许REM单位增长到的十进制数字。
          minPixelValue: 1, //设置要替换的最小像素值
        }),
        autoprefixer({
          overrideBrowserslist: [
            "Android 4.1",
            "iOS 7.1",
            "Chrome > 31",
            "ff > 31",
            "ie >= 8",
            "last 10 versions", // 所有主流浏览器最近10版本用
          ],
          grid: true
        }),
      ],
    },
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler'
      }
    }
  },
  define: {
    __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'true'
  }
})
